//
//  BudgetPieTopView.m
//  BaseProject
//
//  Created by mac on 2020/10/27.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BudgetPieTopView.h"
#import "BudgetEditeVC.h"
#import "BudgetHomeVC.h"
#import "BudgetPiePieView.h"
#import "BudgetPieCollectionView.h"
@interface BudgetPieTopView ()
@property(nonatomic,strong)UILabel *moneyLab;
@property(nonatomic,strong)UILabel *tipLab;
@property(nonatomic,strong)UIButton *leftButton;
@property(nonatomic,strong)UIButton *rightButton;
@property(nonatomic,strong)BudgetPiePieView *pieView;
@property(nonatomic,strong)BudgetPieCollectionView *collectionView;
@end

@implementation BudgetPieTopView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)createProperty
{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5*ADAPTER_WIDTH;
    self.layer.masksToBounds = YES;
}
- (void)createUI
{
    self.moneyLab.text = @"";
    self.tipLab.text = @"预算总额";
    [self pieView];
    self.collectionView.top = self.pieView.bottom + 20*ADAPTER_WIDTH;
    [self leftButton];
    [self rightButton];
}
- (void)setTotalMoney:(NSInteger)totalMoney
{
    _totalMoney = totalMoney;
    self.moneyLab.text = [NSString stringWithFormat:@"¥%ld",totalMoney];
}
- (void)setDataSource:(NSMutableArray<BudgetPieItemModel *> *)dataSource
{
    _dataSource = dataSource;
    //1.allObjArray
    NSMutableArray *allObjArray = [NSMutableArray array];
    for (NSInteger index = 0; index < self.dataSource.count; index ++) {
        BudgetPieItemModel *indexModel = self.dataSource[index];
        for (NSInteger flag = 0; flag < indexModel.dataSource.count; flag ++) {
            BudgetPieItemModel *flagModel = indexModel.dataSource[flag];
            if (flagModel.isSelected) {
                [allObjArray addObject:flagModel];
            }
        }
    }
    //2.重组
    NSMutableArray *valueArray = [NSMutableArray array];
    NSMutableArray *TitleArray = [NSMutableArray array];
    NSMutableArray *ccolorArray = [NSMutableArray array];
    for (NSInteger index = 0; index < allObjArray.count; index ++) {
        BudgetPieItemModel *itemModel = allObjArray[index];
        [valueArray addObject:[NSString stringWithFormat:@"%ld",itemModel.value]];
        [TitleArray addObject:itemModel.title];
        [ccolorArray addObject:itemModel.color];
    }
    //3.刷新饼图
    if (valueArray.count > 0) {
        self.pieView.valueDataArr = valueArray;
        self.pieView.valueDataTitlesArr = @[];
        self.pieView.fillColorArray = ccolorArray;
        self.pieView.ringTitle = @"";
        self.pieView.ringDescription = @"";
        [self.pieView showAnimation];
        [self.pieView setNeedsDisplay];
    }
    //4.刷新collectionView
    self.collectionView.dataSource = allObjArray;
    
    //5.UI
    self.rightButton.top = self.collectionView.bottom + 20*ADAPTER_WIDTH;
    self.leftButton.top = self.collectionView.bottom + 20*ADAPTER_WIDTH;
    self.height = self.rightButton.bottom + 22*ADAPTER_WIDTH;
}
#pragma mark - click
- (void)clickItemButton:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
        {
            BudgetEditeVC *vc = [[BudgetEditeVC alloc]init];
            vc.dataSource = self.dataSource;
            WEAKSELF
            [vc setSureBlock:^{
                !weakSelf.refreshBlock ?: weakSelf.refreshBlock ();
            }];
            [self.parentVC.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            BudgetHomeVC *vc = [[BudgetHomeVC alloc]init];
            WEAKSELF
            [vc setCallBackTotalMoney:^(NSInteger totalMoney) {
                !weakSelf.callBackTotalMoney ?: weakSelf.callBackTotalMoney (totalMoney);
            }];
            [self.parentVC.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma mark - lazy懒加载
- (UILabel *)moneyLab
{
    if (!_moneyLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightBold];
        lab.textColor = RGB(250, 83, 76);
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, 26*ADAPTER_WIDTH, self.width - 20*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
        _moneyLab = lab;
    }
    return _moneyLab;
}
- (UILabel *)tipLab
{
    if (!_tipLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = RGB(153, 153, 153);
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, 54*ADAPTER_WIDTH, self.width - 20*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _tipLab = lab;
    }
    return _tipLab;
}
- (UIButton *)leftButton
{
    if (!_leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"修改项目" forState:UIControlStateNormal];
        [button setTitleColor:RGB(250, 83, 76) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        button.tag = 0;
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake((self.width - 240*ADAPTER_WIDTH - 18*ADAPTER_WIDTH) * 0.5, self.height - 58*ADAPTER_WIDTH, 120*ADAPTER_WIDTH, 36*ADAPTER_WIDTH);
        button.layer.cornerRadius = 18*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = RGB(138, 138, 138).CGColor;
        button.layer.borderWidth = 0.5;
        button.backgroundColor = [UIColor whiteColor];
        _leftButton = button;
    }
    return _leftButton;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"重置预算" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        button.tag = 1;
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(self.leftButton.right + 18*ADAPTER_WIDTH, self.height - 58*ADAPTER_WIDTH, 120*ADAPTER_WIDTH, 36*ADAPTER_WIDTH);
        button.layer.cornerRadius = 18*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = RGB(138, 138, 138).CGColor;
        button.layer.borderWidth = 0.5;
        button.backgroundColor = RGB(250, 83, 76);
        _rightButton = button;
    }
    return _rightButton;
}
//高度160
- (BudgetPiePieView *)pieView
{
    if (!_pieView) {
        BudgetPiePieView *view = [[BudgetPiePieView alloc]initWithFrame:CGRectMake(0, 80*ADAPTER_WIDTH, self.width, 160)];
        [self addSubview:view];
        view.redius = (160 - 40 * 2) / 2;
        view.ringWidth = 40;
        //view.backgroundColor = [UIColor redColor];
        _pieView = view;
    }
    return _pieView;
}
- (BudgetPieCollectionView *)collectionView
{
    if (!_collectionView) {
        BudgetPieCollectionView *view = [[BudgetPieCollectionView alloc]initWithFrame:CGRectMake(0, 0, self.width, 0)];
        [self addSubview:view];
        _collectionView = view;
    }
    return _collectionView;
}
@end
